package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.view.ViewGroup;
import com.jwdf.uda.R;
import com.jwdf.uda.bean.ExtraBaseQuestion;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.event.FollowStatusEvent;
import com.jwdf.uda.ui.adapter.item.ReplyDetailItem;
import com.jwdf.uda.ui.adapter.viewholder.ReplyDescImageViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.ReplyDescTextViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.ReplyMainCreateViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.ReplyMainViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.ReplySectionLikeViewHolder;
import com.jwdf.uda.ui.adapter.viewholder.ReplyThumbLikeViewHolder;
import com.jwdf.uda.utils.StringUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/19.
 */
public class ReplyDetailAdapter extends AbsAdapter<ReplyDetailItem> {
    //    private ReplyDetailItem.ReplyTab mCurrentTab = ReplyDetailItem.ReplyTab.COMMENT;
    private ExtraBaseQuestion mBaseQuestion;
    private ArrayList<String> descImages;
    private List<ReplyDetailItem> mItems = new ArrayList<>();
//    private List<ReplyDetailItem> mItemsTab0 = new ArrayList<>();
//    private List<ReplyDetailItem> mItemsTab1 = new ArrayList<>();

    public ReplyDetailAdapter(Context context) {
        super(context);
    }

    public void setExtraBaseQuestion(ExtraBaseQuestion baseQuestion) {
        mBaseQuestion = baseQuestion;
    }

//    public void setCurrentTab(ReplyDetailItem.ReplyTab currentTab) {
//        mCurrentTab = currentTab;
//        if (mCurrentTab == ReplyDetailItem.ReplyTab.LIKE && mItemsTab1.size() <= 0) {
//            EventBusUtil.post(new ReplyDetailEvent(ReplyDetailEvent.EVENT_REFRESH_LIKE));
//        }
//        for (int i = 0; i < mItems.size(); i++) {
//            ReplyDetailItem item = mItems.get(i);
//            if (item.itemType == ReplyDetailItem.TYPE_TITLE_TAB) {
//                item.currentTab = currentTab;
//            }
//        }
//        notifyDataSetChanged();
//    }

//    public ReplyDetailItem.ReplyTab getCurrentTab() {
//        return mCurrentTab;
//    }
//
//    public int getCommentOffset() {
//        return mItemsTab0.size();
//    }
//
//    public int getLikeOffset() {
//        return mItemsTab1.size();
//    }

    @Override
    public ReplyDetailItem getItem(int position) {
//        int index = position - mItems.size();
//        if (index < 0) {
        return mItems.get(position);
//        } else {
//            if (mCurrentTab == ReplyDetailItem.ReplyTab.COMMENT) {
//                return mItemsTab0.get(index);
//            } else {
//                return mItemsTab1.get(index);
//            }
//        }
    }

    @Override
    public int getCount() {
//        int size = mCurrentTab == ReplyDetailItem.ReplyTab.COMMENT ? mItemsTab0.size() : mItemsTab1.size();
        return mItems.size();
    }

//    private void addCommentItem(PbBean.PbBaseComment comment) {
//        ReplyDetailItem item;
//        List<PbBean.PbImage> imageList = comment.getImageListList();
//        if (imageList == null || imageList.size() <= 0) {
//            item = new ReplyDetailItem(ReplyDetailItem.TYPE_COMMENT_TXT);
//        } else {
//            item = new ReplyDetailItem(ReplyDetailItem.TYPE_COMMENT_IMAGE);
//        }
//        item.comment = comment;
//        item.commentId = comment.getId();
//        item.isLikeComment = comment.getIsLike();
//        item.commentLikeCount = comment.getLikeCount();
//        if (!TextUtils.isEmpty(StringUtils.filterNullAndTrim(item.comment.getContent())) ||
//                (item.comment.getImageListList() != null && !item.comment.getImageListList().isEmpty())) {
//            mItemsTab0.add(item);
//        }
//    }

//    public void refreshCommentCount(int commentCount) {
//        for (ReplyDetailItem item : mItems) {
//            if (item.itemType == ReplyDetailItem.TYPE_TITLE_TAB) {
//                item.commentCount = commentCount;
//                break;
//            }
//        }
//    }

//    //刷新评论
//    public void refreshComments(List<PbBean.PbBaseComment> comments) {
//        mItemsTab0.clear();
//        if (comments != null && comments.size() > 0) {
//            for (int i = 0; i < comments.size(); i++) {
//                PbBean.PbBaseComment comment = comments.get(i);
//                addCommentItem(comment);
//            }
//        }
//        notifyDataSetChanged();
//    }

//    //加载更多评论
//    public void loadMoreComments(List<PbBean.PbBaseComment> comments) {
//        if (comments != null && comments.size() > 0) {
//            for (int i = 0; i < comments.size(); i++) {
//                PbBean.PbBaseComment comment = comments.get(i);
//                addCommentItem(comment);
//            }
//        }
//        notifyDataSetChanged();
//    }

//    public void refreshLikeCount(int likeCount) {
//        for (ReplyDetailItem item : mItems) {
//            if (item.itemType == ReplyDetailItem.TYPE_TITLE_TAB) {
//                item.likeCount = likeCount;
//                break;
//            }
//        }
//    }

    //刷新zan
    public void refreshLikes(int answerId, List<PbBean.PbUserSimple> likes, int likeCount) {
        if (likes != null && likes.size() > 0) {
            addLikeSectionItem(likeCount);
            addThumbUserLikeItem(answerId, likes);
        }
        notifyDataSetChanged();
    }

//    public void onEventMainThread(ReplyDetailItemClickEvent event) {
//        if (event != null) {
//            ReplyDetailItem eventItem = event.getItem();
//            if (ReplyDetailItemClickEvent.EVENT_COMMENT_LIKE_CLICK == event.getEvent()) {
//                for (ReplyDetailItem item : mItemsTab0) {
//                    if (item.commentId == eventItem.commentId) {
//                        item.isLikeComment = eventItem.isLikeComment;
//                        item.commentLikeCount = eventItem.commentLikeCount;
//                        notifyDataSetChanged();
//                        break;
//                    }
//                }
//            }
//        }
//    }

    public void onEventMainThread(FollowStatusEvent event) {
        if (event != null) {
            for (ReplyDetailItem item : mItems) {
                if (ReplyDetailItem.TYPE_REPLY_MAIN == item.itemType) {
                    item.isFollowed = event.isFollowed;
                    notifyDataSetChanged();
                    break;
                }
            }
        }
    }

//    //加载更多zan
//    public void loadMoreLikes(List<PbBean.PbUserSimple> likes) {
//        if (likes != null && likes.size() > 0) {
//            for (int i = 0; i < likes.size(); i++) {
//                PbBean.PbUserSimple user = likes.get(i);
//                addLikeItem(user);
//            }
//        }
//        notifyDataSetChanged();
//    }

    private void addReplyMainInfoItem(PbBean.PbResQuestionFindAnswer detail) {
        ReplyDetailItem item = new ReplyDetailItem(ReplyDetailItem.TYPE_REPLY_MAIN);
        item.userInfo = detail.getAnswerInfo().getUserInfo();
        PbBean.PbSignUserFollowType type = item.userInfo.getFollowType();
        switch (type) {
            case USER_FOLLOW_TYPE_FOLLOW:
            case USER_FOLLOW_TYPE_BOTH:
                item.isFollowed = true;
                break;
            case USER_FOLLOW_TYPE_FANS:
            case USER_FOLLOW_TYPE_NONE:
                item.isFollowed = false;
                break;
        }
        item.quizTitle = detail.getQuestionTitle();
        item.questionId = detail.getQuestionId();
        item.extraBaseQuestion = mBaseQuestion;
        if (detail.getVoteOption() != null) {
            item.voteOptionType = detail.getAnswerInfo().getVoteOption();
        }
        mItems.add(item);
    }

    private void addDescImages(PbBean.PbBaseQuestionAnswer answer) {
        List<PbBean.PbBaseResDescription> descriptions = answer.getDescInfoList();
        descImages = new ArrayList<>();
        for (PbBean.PbBaseResDescription imageText : descriptions) {
            if (PbBean.PbSignBaseDescriptionType.DESCRIPTION_TPYE_IMG == imageText.getDescType()) {
                PbBean.PbImage image = imageText.getDescImage();
                descImages.add(image.getUrl());
            }
        }
    }

    private void addReplyMainDescItem(PbBean.PbBaseQuestionAnswer answer) {
        addDescImages(answer);
        List<PbBean.PbBaseResDescription> descriptions = answer.getDescInfoList();
        int index = 0;
        for (PbBean.PbBaseResDescription imageText : descriptions) {
            if (PbBean.PbSignBaseDescriptionType.DESCRIPTION_TPYE_TEXT == imageText.getDescType()) {
                ReplyDetailItem item = new ReplyDetailItem(ReplyDetailItem.TYPE_REPLY_DESC_TXT);
                item.answerDescTxt = StringUtils.filterNullAndTrim(imageText.getDescText());
                mItems.add(item);
            } else {
                PbBean.PbImage image = imageText.getDescImage();
                ReplyDetailItem item = new ReplyDetailItem(ReplyDetailItem.TYPE_REPLY_DESC_IMAGE);
                item.answerDescImage = image.getUrl();
                item.imageWidth = image.getWidth();
                item.imageHeight = image.getHeight();
                item.imageIndex = index++;
                item.imageUrls = descImages;
                mItems.add(item);
            }
        }
        addCreateTimeItem(answer);
    }

    private void addCreateTimeItem(PbBean.PbBaseQuestionAnswer answer) {
        ReplyDetailItem item = new ReplyDetailItem(ReplyDetailItem.TYPE_REPLY_CREATE_TIME);
        item.createTime = answer.getCreateTime();
        mItems.add(item);
    }

    private void addLikeSectionItem(int likeCount) {
        ReplyDetailItem item = new ReplyDetailItem(ReplyDetailItem.TYPE_REPLY_LIKE_SECTION);
        item.likeCount = likeCount;
        mItems.add(item);
    }

    private void addThumbUserLikeItem(int answerId, List<PbBean.PbUserSimple> likes) {
        ReplyDetailItem item = new ReplyDetailItem(ReplyDetailItem.TYPE_REPLY_USER_LIKE);
        item.likeInfos = likes;
        item.answerId = answerId;
        mItems.add(item);
    }

//    private void addReplyTitleTabItem(PbBean.PbResQuestionFindAnswer detail) {
//        ReplyDetailItem item = new ReplyDetailItem(ReplyDetailItem.TYPE_TITLE_TAB);
//        item.commentCount = detail.getAnswerInfo().getCommentCount();
//        item.currentTab = mCurrentTab;
//        item.likeCount = detail.getAnswerInfo().getLikeCount();
//        mItems.add(item);
//    }
//
//    private void addLikeItem(PbBean.PbUserSimple like) {
//        ReplyDetailItem item = new ReplyDetailItem(ReplyDetailItem.TYPE_LIKE);
//        item.likeInfo = like;
//        mItemsTab1.add(item);
//    }

    public void setDetailData(PbBean.PbResQuestionFindAnswer detail) {
        mItems.clear();

        addReplyMainInfoItem(detail);
        addReplyMainDescItem(detail.getAnswerInfo());
        List<PbBean.PbUserSimple> likes=detail.getLikeListList();
        if (likes != null && likes.size() > 0) {
            addLikeSectionItem(detail.getLikeListCount());
            addThumbUserLikeItem(detail.getAnswerInfo().getAnswerId(), likes);
        }
//        addReplyTitleTabItem(detail);

//        List<PbBean.PbBaseComment> comments = detail.getCommentListList();
//        if (comments != null && comments.size() > 0) {
//            for (int i = 0; i < comments.size(); i++) {
//                PbBean.PbBaseComment comment = comments.get(i);
//                addCommentItem(comment);
//            }
//        }

        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).itemType;
    }

    @Override
    public int getViewTypeCount() {
        return 6;
    }

    @Override
    protected AbsViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        switch (itemType) {
            case ReplyDetailItem.TYPE_REPLY_MAIN:
                return new ReplyMainViewHolder(mInflater.inflate(R.layout.item_reply_main, parent, false));
            case ReplyDetailItem.TYPE_REPLY_DESC_TXT:
                return new ReplyDescTextViewHolder(mInflater.inflate(R.layout.item_quiz_main_desc_txt, parent, false));
            case ReplyDetailItem.TYPE_REPLY_DESC_IMAGE:
                return new ReplyDescImageViewHolder(mInflater.inflate(R.layout.item_quiz_main_desc_image, parent, false));
            case ReplyDetailItem.TYPE_REPLY_CREATE_TIME:
                return new ReplyMainCreateViewHolder(mInflater.inflate(R.layout.item_reply_main_time, parent, false));
            case ReplyDetailItem.TYPE_REPLY_LIKE_SECTION:
                return new ReplySectionLikeViewHolder(mInflater.inflate(R.layout.view_like_section, parent, false));
            case ReplyDetailItem.TYPE_REPLY_USER_LIKE:
                return new ReplyThumbLikeViewHolder(mInflater.inflate(R.layout.item_reply_thumb_like, parent, false));
        }
        return null;
    }

}
